﻿using System;
using System.Runtime.InteropServices;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace TD
{
    partial class InputManager
    {
        private KeyboardState previousKeyboardState;
        private KeyboardState currentKeyboardState;

        private void InitializeKeyboard()
        {
            // Initialize them when starting and with the same values.
            previousKeyboardState = Keyboard.GetState();
            currentKeyboardState = previousKeyboardState;
        }

        private void UpdateKeyboard(GameTime gameTime)
        {
            // Update both states.
            previousKeyboardState = currentKeyboardState;
            currentKeyboardState = Keyboard.GetState();
        }

        /// <summary>
        /// Determine whether or not the specified key is down.
        /// </summary>
        /// <param name="key">The key from whom we want to know if it is currently down.</param>
        /// <returns>True when the key is down. False when the key is up.</returns>
        public bool IsKeyDown(Keys key)
        {
            return (currentKeyboardState.IsKeyDown(key));
        }

        /// <summary>
        /// Determine whether or not the specified key is up.
        /// </summary>
        /// <param name="key">The key from whom we want to know if it is currently up.</param>
        /// <returns>True when the key is up. False when the key is down.</returns>
        public bool IsKeyUp(Keys key)
        {
            return (currentKeyboardState.IsKeyUp(key));
        }

        /// <summary>
        /// Determine whether or not the specified key has been pressed.
        /// </summary>
        /// <param name="key">The key from whom we want to know whether it has been pressed or not.</param>
        /// <returns>True when it has been pressed. False when it has not been pressed.</returns>
        public bool IsKeyPressed(Keys key)
        {
            return (currentKeyboardState.IsKeyUp(key) &&
                previousKeyboardState.IsKeyDown(key));
        }
    }
}
